﻿using System;
using fastCSharp;

namespace diantou.examImageServer
{
    /// <summary>
    /// 考试用户图片服务
    /// </summary>
    class console : fastCSharp.diagnostics.consoleLog
    {
        /// <summary>
        /// 考试用户图片服务
        /// </summary>
        private diantou.examImageServer.server server;
        /// <summary>
        /// 考试用户图片服务实例
        /// </summary>
        private server.tcpServer cacheServer;
        /// <summary>
        /// 当前进程是否正在尝试忽略考试用户图片服务端操作
        /// </summary>
        private bool isTryIgnoreServer;
        /// <summary>
        /// 启动考试用户图片服务
        /// </summary>
        protected override void start()
        {
            isTryIgnoreServer = true;
            try
            {
                using (server.tcpClient client = new server.tcpClient())
                {
                    client.TcpIgnoreGroup(int.MaxValue);
                    client.TcpIgnoreGroup(0);
                }
            }
            catch (Exception error)
            {
                output(error);
            }
            finally
            {
                isTryIgnoreServer = false;
                cache();
            }
        }
        /// <summary>
        /// 启动考试用户图片服务
        /// </summary>
        private void cache()
        {
            try
            {
                if ((cacheServer = new server.tcpServer(null, server = new server())).Start())
                {
                    setTcpIgnoreGroup(() => cacheServer, () => isTryIgnoreServer);
                    output("Image Server 启动成功");
                    return;
                }
            }
            catch (Exception error)
            {
                output(error);
            }
            cacheServer = null;
            output("Image Server 启动失败");
            fastCSharp.threading.timerTask.Default.Add(cache, date.NowSecond.AddSeconds(1));
        }
        /// <summary>
        /// 关闭考试用户图片服务
        /// </summary>
        protected override void dispose()
        {
            pub.Dispose(ref cacheServer);
            server = null;
        }
    }
}
